/********************************
 *版权所有 CopyRight(c) 快程乐码信息有限公司所有，未经授权，不得复制、转发
 */
package com.yjx.test.dozer.utils;

import org.dozer.Mapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.*;

/************
 *@Author yejx
 *@version v1.0
 *@create 2020/6/29 11:00
 *@Description TODO
 *
 */
@Component
public class DataConvertor {

    @Autowired
    private Mapper mapper;

    /**
     * List集合
     *
     * @param source 数据源
     * @param clz    需要被赋值的的对象
     * @param <T>
     * @param <S>
     * @return
     */
    public <T, S> List<T> convertor(List<S> source, Class<T> clz) {
        if (source == null) return null;
        List<T> map = new ArrayList<>();
        for (S s : source) {
            map.add(mapper.map(s, clz));
        }
        return map;
    }

    /**
     * 两个集合映射成一个集合
     *
     * @param source  数据源一
     * @param source2 数据源二
     * @param clazz   target.class
     * @param <T>
     * @param <M>
     * @param <S>
     * @return
     */
    public <T, M, S> List<T> convertor(List<S> source, List<M> source2, Class<T> clazz) throws IllegalAccessException, InstantiationException {
        if (source == null || source2 == null) return null;
        if (source.size() != source2.size()) return null;
        List<T> map = new ArrayList<>();
        for (int i = 0; i < source.size(); i++) {
            T t = clazz.newInstance();
            mapper.map(source.get(0), t);
            mapper.map(source2.get(0), t);
            map.add(t);
        }
        return map;
    }

    //   public <S, T> Map


    /**
     * Set集合
     *
     * @param source 数据源
     * @param clz    需要被赋值的的对象
     * @param <T>
     * @param <S>
     * @return
     */
    public <T, S> Set<T> convertor(Set<S> source, Class<T> clz) {
        if (source == null) return null;
        Set<T> set = new TreeSet<>();
        for (S s : source) {
            set.add(mapper.map(s, clz));
        }
        return set;
    }

    /**
     * 例：Map<Integer,StudentDTO>  转  Map<Integer,StudentVo>
     *
     * @param sourceMap 传入的 Map
     * @param clz
     * @param <T>
     * @param <M>
     * @param <S>
     * @return Map<M, T>
     */
    public <T, M, S> Map<M, T> convertor(Map<M, S> sourceMap, Class<T> clz) {
        if (sourceMap == null || sourceMap.size() <= 0) return null;
        Map<M, T> targetMap = new HashMap<>();
        for (Map.Entry<M, S> entry : sourceMap.entrySet()) {
            T t = mapper.map(entry.getValue(), clz);
            targetMap.put(entry.getKey(), t);
        }
        return targetMap;
    }

   /* public <T,M,S> Map<M,T> convertor(Map<M,S> sourceMap, Class<T> clz){
        if(sourceMap == null || sourceMap.size() <= 0) return null;
        Map<M, T> targetMap = new HashMap<>();
        sourceMap.forEach((x,y) ->{
              targetMap.put(x,mapper.map(y, clz));
        });
        return targetMap;
    }
*/

    /**
     * 实体转换
     *
     * @param source 数据源
     * @param clz    需要被赋值的的对象
     * @param <T>
     * @param <S>
     * @return
     */
    public <T, S> T convertor(S source, Class<T> clz) {
        if (source == null) return null;
        return mapper.map(source, clz);
    }

    /**
     * @param source
     * @param object
     */
    public void convertor(Object source, Object object) {
        mapper.map(source, object);
    }

    /**
     * @param source
     * @param object
     * @param <T>
     */
    public <T> void copyConvertor(T source, Object object) {
        mapper.map(source, object);
    }


}
